<template>
  <div>
    <input placeholder="输入关键字搜索" v-model.trim="searchKeyword" />
    <table class="my-table">
      <thead>
        <tr>
          <th>就诊日期</th>
          <th>医生姓名</th>
          <th>诊断结果</th>
          <th>处方信息</th>
          <th>操作</th>
        </tr>
      </thead>
      <tbody>
        <tr v-for="(item, index) in records" :key="index">
          <td>{{ item.date }}</td>
          <td>{{ item.doctor }}</td>
          <td>{{ item.diagnosis }}</td>
          <td>{{ item.prescription }}</td>
          <td @click="reset(index)">详情</td>
        </tr>
      </tbody>
      <h2 v-show="records.length == 0">搜索内容不存在</h2>
    </table>
    <detailsPage
      @close="close"
      v-show="showDetails"
      :isShow="showDetails"
      :reset="records[showIndex]"
    />
  </div>
</template>

<style scoped>
.my-table {
  border-collapse: collapse;
  width: 100%;
}

.my-table td,
.my-table th {
  border: 1px solid #ddd;
  padding: 8px;
  text-align: left;
}

.my-table th {
  background-color: #f2f2f2;
}
</style>

<script>
import detailsPage from "./detailsPage.vue";
export default {
  components: { detailsPage },
  data() {
    return {
      records: [
        {
          date: "2022-01-01",
          doctor: "张三",
          diagnosis: "感冒",
          prescription: "感冒药",
        },
        {
          date: "2022-02-01",
          doctor: "李四",
          diagnosis: "头疼",
          prescription: "止疼药",
        },
        {
          date: "2022-03-01",
          doctor: "王五",
          diagnosis: "腰痛",
          prescription: "止痛贴",
        },
      ],
      searchKeyword: "",
      showDetails: false,
      showIndex: "",
    };
  },
  methods: {
    reset(index) {
      this.showIndex = index;
      this.showDetails = true;
    },
    close() {
      this.showDetails = false;
    },
  },
  watch: {
    searchKeyword(newValue) {
      // 搜索功能待实现
      if (newValue === "") {
        this.records = [
          {
            date: "2022-01-01",
            doctor: "张三",
            diagnosis: "感冒",
            prescription: "感冒药",
          },
          {
            date: "2022-02-01",
            doctor: "李四",
            diagnosis: "头疼",
            prescription: "止疼药",
          },
          {
            date: "2022-03-01",
            doctor: "王五",
            diagnosis: "腰痛",
            prescription: "止痛贴",
          },
        ];
      } else {
        this.records = this.records.filter((record) => {
          return (
            record.date.includes(newValue) ||
            record.doctor.includes(newValue) ||
            record.diagnosis.includes(newValue) ||
            record.prescription.includes(newValue)
          );
        });
      }
    },
  },
};
</script>
